import http from '@/utils/http-util'

let BASE_SCHEDULED_CONTENT = process.env.WEB_CONTEXT_PATH + 'scheduled'
/** 获取调度列表 **/
export function getScheduledListApi(data = {}) {
  return http({
    url: BASE_SCHEDULED_CONTENT + '/list',
    method: 'post',
    data: http.bodyData({
      'page': data.page || 1,
      'pageSize': data.pageSize || 20,
      'sort': {
        'field': data.sortField,
        'rule': data.sortRule === undefined ? 1 : data.sortRule
      },
      'status': data.status || '',
      'name': data.name || ''
    })
  })
}

/** 删除调度 **/
export function deleteScheduledApi(id = 0) {
  return http({
    url: BASE_SCHEDULED_CONTENT + '/' + id,
    method: 'delete',
    params: http.queryParam()
  })
}

/** 获取调度信息 **/
export function getScheduledInfoApi(id = 0) {
  return http({
    url: BASE_SCHEDULED_CONTENT + '/' + id,
    method: 'get',
    params: http.queryParam(),
    loading: false
  })
}

/** 保存 **/
export function saveScheduledApi(data = {}) {
  return http({
    url: BASE_SCHEDULED_CONTENT,
    method: 'post',
    data: http.bodyData({
      name: data.name,
      beanName: data.beanName,
      cron: data.cron,
      initStartFlag: data.initStartFlag,
      failNotice: data.failNotice,
      protect: data.protect,
      remark: data.remark
    }),
    loading: false
  })
}

/** 修改 **/
export function updateScheduledApi(data = {}) {
  if (!data.id || data.id === 0 || data.id === '0') return
  return http({
    url: BASE_SCHEDULED_CONTENT + '/' + data.id,
    method: 'put',
    data: http.bodyData({
      name: data.name,
      beanName: data.beanName,
      cron: data.cron,
      initStartFlag: data.initStartFlag,
      failNotice: data.failNotice,
      protect: data.protect,
      remark: data.remark
    }),
    loading: false
  })
}

/** 修改调度状态 **/
export function updateScheduledStatusApi(id = 0, status = 0) {
  return http({
    url: BASE_SCHEDULED_CONTENT + '/' + id,
    method: 'patch',
    params: http.queryParam({ status }),
    loading: false
  })
}

/** 获取调度日志 **/
export function getScheduledLogApi(data = {}) {
  return http({
    url: BASE_SCHEDULED_CONTENT + '/' + data.id + '/log',
    method: 'get',
    params: http.queryParam({
      'page': data.page || 1,
      'pageSize': data.pageSize || 10,
      'demarcateTime': data.demarcateTime,
      'init': data.init
    })
  })
}
